package com.ruoyi.system.apiController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.YySchedule;
import com.ruoyi.system.service.IYyHospitalDepartmentService;
import com.ruoyi.system.service.IYyScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 医生排班接口
 */
@RestController
@RequestMapping("/api/schedule")
public class YyScheduleApiController {
    @Autowired
    private IYyScheduleService yyScheduleService;
    @Autowired
    private IYyHospitalDepartmentService yyHospitalDepartmentService;

    /**
     * 查询所有医生排班信息
     * @param
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(name = "doctorId",required = false) Long doctorId,
                           @RequestParam(name = "departmentId",required = false) Long departmentId,
                           @RequestParam(name = "scheduleDate",required = false) String scheduleDate) throws Exception{
        YySchedule yySchedule = new YySchedule();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (scheduleDate!=null){
            Date date = format.parse(scheduleDate);
            yySchedule.setScheduleDate(date);
        }
        yySchedule.setDoctorId(doctorId);
        yySchedule.setDepartmentId(departmentId);
        List<Map<String,Object>> list = yyScheduleService.selectYyScheduleList2(yySchedule);
        return AjaxResult.success(list);
    }

    /**
     * 根据医生id和排班日期查看个人排班信息以及预约情况
     */
    @GetMapping("/getSchedule")
    public AjaxResult getSchedule(Integer id,String date){
        List<Map<String,Object>> list = yyScheduleService.selectYyScheduleList3(id,date);
        return AjaxResult.success(list);
    }

    /**
     * 添加医生排班
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestParam(name = "doctorId") Long doctorId,
                          @RequestParam(name = "departmentId") Long departmentId,
                          @RequestParam(name = "scheduleDate") String scheduleDate) throws Exception{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(scheduleDate);
        YySchedule yySchedule = new YySchedule();
        yySchedule.setDoctorId(doctorId);
        yySchedule.setDepartmentId(departmentId);
        yySchedule.setScheduleDate(date);
        int i = yyScheduleService.insertYySchedule(yySchedule);
        if(i>0){
            return AjaxResult.success("添加成功");
        }else{
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 修改排班信息
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestParam(name = "doctorId", required = false) Long doctorId,
                          @RequestParam(name = "departmentId",required = false) Long departmentId,
                          @RequestParam(name = "scheduleDate",required = false) String scheduleDate,
                             @RequestParam(name = "id",required = true) Long id) throws Exception{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        YySchedule yySchedule = new YySchedule();
        if (scheduleDate!=null){
            Date date = dateFormat.parse(scheduleDate);
            yySchedule.setScheduleDate(date);
        }
        yySchedule.setDoctorId(doctorId);
        yySchedule.setDepartmentId(departmentId);
        yySchedule.setId(id);
        int i = yyScheduleService.updateYySchedule(yySchedule);
        if(i>0){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 查询所有科室
     */
    @GetMapping("/department")
    public AjaxResult department(){
        List<Map<String,Object>> list = yyHospitalDepartmentService.selectYyScheduleList();
        return AjaxResult.success(list);
    }

    /**
     * 删除排版情况
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String ids){
        int i = yyScheduleService.deleteYyScheduleByIds(ids);
        if(i>0){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }

}
